<?php

namespace app\admin\service;

use app\admin\dao\UserPersonnelDao;
use app\admin\model\UM as UM;
use app\common\model\UserPersonnel;
use ShidianUtil\Exception\DataNotExistException;
use think\exception\ValidateException;

/**
 * Class PersonnelSmsService
 * @package app\admin\service
 */
class PersonnelSmsService
{
    // dao
    private UserPersonnelDao $userPersonnelDao;

    private PersonnelService $personnelService;

    public function __construct(UserPersonnelDao $userPersonnelDao)
    {
        $this->userPersonnelDao = $userPersonnelDao;
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     */
    public function modify(UM $user, int $id, array $params): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = UserPersonnel::field(true)
            ->with(['member', 'company'])
            ->where('id', $id)
            ->find();

        if (is_null($record)) {
            throw new DataNotExistException('该员工不存在或已被删除');
        }

        if ($record->member) {
            throw new ValidateException('该员工已绑定，请勿重复邀请');
        }

        // 邀请绑定
        $mobile = $record->mobile;
        $companyName = $record->company->name;
        $this->personnelService->inviteBind($mobile, $companyName, $record->id);

    }

}
